/*
 * DataListModel.cpp
 *
 *  Created on: Jan 8, 2009
 *      Author: sez
 */

#include <DndData/DndData.h>

#include "DataListModel.h"

template <typename T>
DataListModel<T>::DataListModel(const char *names[], int size, QIcon icon, QObject *parent)
	: QStandardItemModel(parent)
{
	for (int i = 0; i < size; ++i)
	{
		QStandardItem *item = new QStandardItem(names[i]);
		appendRow(item);
		item->setCheckable(true);
		item->setIcon(icon);
	}
}

template <typename T>
DataListModel<T>::~DataListModel()
{
}

template <typename T>
QList<T> DataListModel<T>::selected() const
{
	int rows = rowCount();
	QList<T> result;
	for (int i = 0; i < rows; ++i)
	{
		if (item(i)->checkState() == Qt::Checked)
			result << static_cast<T>(i);
	}
	return result;
}

template <typename T>
void DataListModel<T>::setSelected(QList<T> data)
{
	int rows = rowCount();
	for (int i = 0; i < rows; ++i)
		item(i)->setCheckState(Qt::Unchecked);
	for (int i = 0; i < data.size(); ++i)
		item(data[i])->setCheckState(Qt::Checked);
}

template class DataListModel<DndData::Language>;
template class DataListModel<DndData::SkillTypes>;
template class DataListModel<DndData::Ability>;
template class DataListModel<DndData::ClassName>;
template class DataListModel<DndData::RaceName>;

LanguagesModel *makeLanguagesModel(QObject *parent)
{
	return new LanguagesModel(DndData::LANGUAGE_NAMES, DndData::NUM_LANGUAGES,
			QIcon(":/icons/icons/16x16/actions/address-book-new.png"),
			parent);
}

SkillsModel *makeSkillsModel(QObject *parent)
{
	return new SkillsModel(DndData::SKILL_TYPES, DndData::NUM_SKILL_TYPES,
			QIcon(":/icons/icons/16x16/actions/edit-clear.png"),
			parent);
}

AbilitiesModel *makeAbilitiesModel(QObject *parent)
{
	return new AbilitiesModel(DndData::ABILITY_NAMES, DndData::NUM_ABILITIES,
			QIcon(":/icons/icons/16x16/actions/format-justify-left.png"),
			parent);
}

SimpleClassModel *makeSimpleClassModel(QObject *parent)
{
	return new SimpleClassModel(DndData::CLASS_NAMES, DndData::NUM_CLASSES,
			QIcon(":/icons/icons/16x16/actions/go-home.png"),
			parent);
}

SimpleRaceModel *makeSimpleRaceModel(QObject *parent)
{
	return new SimpleRaceModel(DndData::RACE_NAMES, DndData::NUM_RACES,
			QIcon(":/icons/icons/16x16/actions/contact-new.png"),
			parent);
}
